<template>
  <div :class="['header-bar', 'header-bar-' + theme]">
    <div class="menu">
      <el-popover
        :width="categoryCount * (150 + 21) + 24"
        trigger="hover"
        :show-arrow="false"
        :offset="22"
        placement="bottom-start"
      >
        <template #reference>
          <router-link to="/" class="iconfont icon-logo">首页</router-link>
        </template>
        <div class="nav-list">
          <div class="nav-part" v-for="index in categoryCount" :key="index">
            <router-link
              class="nav-item"
              v-for="item in categoryStore.categoryList.slice((index - 1) * count, index * count)"
              :key="item.categoryId"
              :to="`/v/${item.categoryCode}`"
            >
              <span class="icon" v-if="item.icon">
                <img :src="`${homeStore.sourcePath}${item.icon}`" alt="" />
              </span>
              <span class="category-name">{{ item.categoryIdName }}</span>
            </router-link>
          </div>
        </div>
      </el-popover>
    </div>
    <div class="search-body">
      <div class="search-panel">
        <div class="search-panel-inner">
          <div class="input-panel">
            <input type="text" />
            <div class="iconfont icon-search"></div>
          </div>
        </div>
      </div>
    </div>
    <div class="user-panel">
      <div class="user-avatar">
        <!-- 已经登录的显示状态 -->
        <template v-if="homeStore.userInfo && Object.keys(homeStore.userInfo).length > 0">
          <Avatar
            class="avatar"
            :avatar="homeStore.userInfo.avatar"
            :user-id="homeStore.userInfo.userId"
            :width="35"
            :lazy="false"
            @mouseover="getCountInfo"
          ></Avatar>
          <div class="user-info-panel">
            <div class="nick-name">{{ homeStore.userInfo.nickName || '默认用户' }}</div>
            <div class="count-info">
              <div class="count-info-item">
                <div class="count-value">{{ userCountInfo?.focusCount }}</div>
                <div class="count-title">关注</div>
              </div>
              <div class="count-info-item">
                <div class="count-value">{{ userCountInfo?.fansCount }}</div>
                <div class="count-title">粉丝</div>
              </div>
              <div class="count-info-item">
                <div class="count-value">{{ userCountInfo?.currentCoinCount }}</div>
                <div class="count-title">硬币</div>
              </div>
            </div>
            <router-link :to="`/user/${homeStore.userInfo.userId}`" class="item iconfont icon-user">
              <span class="item-name">个人中心</span>
              <span class="iconfont icon-left"></span>
            </router-link>
            <router-link :to="`/ucenter/video`" class="item iconfont icon-play">
              <span class="item-name">投稿管理</span>
              <span class="iconfont icon-left"></span>
            </router-link>
            <div
              style="
                width: 100%;
                padding: 0 15px;
                height: 1px;
                background-color: #ddd;
                margin: 10px 0;
              "
            ></div>
            <div class="logout item iconfont icon-logout" @click="logout">退出登录</div>
          </div>
        </template>
        <!-- 未登录 -->
        <Avatar v-else class="avatar" :width="35" :lazy="false" @click="login"></Avatar>
      </div>
      <div class="user-panel-item" @click="navJump('/message')">
        <div class="iconfont icon-message"></div>
        <div>消息</div>
      </div>
      <div
        class="user-panel-item"
        @click="navJump(`/user/${homeStore.userInfo?.userId}/collection`)"
      >
        <div class="iconfont icon-collection"></div>
        <div>收藏</div>
      </div>
      <div class="user-panel-item" @click="navJump('/history')">
        <div class="iconfont icon-history"></div>
        <div>历史</div>
      </div>
      <div class="user-panel-item" @click="navJump('/ucenter/home')">
        <div class="iconfont icon-light"></div>
        <div>创作中心</div>
      </div>
      <div class="btn-upload" @click="navJump('/ucenter/postVideo')">
        <el-button type="primary" size="large">
          <span class="iconfont icon-upload"></span>
          <span>投稿</span>
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getUserCountInfo, logoutLoginHttpRequest } from '@/services/api/home'
import { useCategoryStore } from '@/stores/category'
import { useHomeStore } from '@/stores/home'
import { confirmMessageBox } from '@/utils/confirm'
import message from '@/utils/message'
import { getLocalImage } from '@/utils/utilsFun'
import { computed, ref, withDefaults } from 'vue'
interface PropsType {
  theme?: 'light' | 'dark'
}

const homeStore = useHomeStore()
const categoryStore = useCategoryStore()
const props = withDefaults(defineProps<PropsType>(), {
  theme: 'light'
})

//登录
const login = () => {
  homeStore.setLogin(true)
}

//获取用户的硬币数量粉丝数量和关注数量
const userCountInfo = ref<{
  fansCount: number
  focusCount: number
  currentCoinCount: number
}>()
const getCountInfo = async () => {
  const countInfo = await getUserCountInfo()
  userCountInfo.value = countInfo.data
}

//退出登录
const logout = () => {
  confirmMessageBox({
    message: '确认要退出登录吗？',
    onConfirm: async () => {
      const res = await logoutLoginHttpRequest()
      if (!res) {
        return
      }
      homeStore.saveUserInfo(null)
      message.success('退出登录成功')
    },
    onCancel: () => {
      console.log('取消')
    }
  })
}

//根据分类的数量来算出多少的宽度 默认每列是5个
const count = 5
const categoryCount = computed(() => {
  return Math.ceil(categoryStore.categoryList.length / count)
})

//跳转页面
const navJump = (path: string) => {
  if (!homeStore.userInfo || Object.keys(homeStore.userInfo).length == 0) {
    homeStore.setLogin(true)
    return
  }
  window.open(path, '_blank')
}
</script>

<style lang="less" scoped>
.header-bar {
  width: 100%;
  height: 64px;
  padding: 0 25px;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  .menu {
    display: flex;
    align-items: center;
    a {
      text-decoration: none;
    }
    .icon-logo {
      font-size: 16px;
      &::before {
        float: left;
        margin-top: -1px;
        font-size: 25px;
        margin-right: 5px;
      }
    }
  }
  .search-body {
    color: #61666d;
    .search-panel {
      margin: 0px auto;
      position: relative;
      max-width: 80%;
      .search-panel-inner {
        width: 100%;
        position: absolute;
        top: 10px;
        left: 0px;
        border: 1px solid #e3e5e7;
        border-radius: 8px;
        overflow: hidden;
        z-index: 1001;
        .input-panel {
          display: flex;
          align-items: center;
          background: #f1f2f3;
          width: 100%;
          input {
            width: 100%;
            border: none;
            background: #f1f2f3;
            border-radius: 5px;
            padding: 8px 5px;
            margin: 3px 10px 3px 10px;
            &:focus {
              outline: none;
            }
          }
          .iconfont {
            font-size: 20px;
            margin-left: 10px;
            color: #2f3238;
            width: 35px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 5px;
            cursor: pointer;
            &:hover {
              background-color: #ddd;
            }
          }
        }
      }
    }
  }
  .user-panel {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    .user-avatar {
      position: relative;
      margin-right: 13px;
      // overflow: hidden;
      width: 35px;
      height: 35px;
      .avatar {
        transition: transform 0.3s;
        position: absolute;
        z-index: 10001;
        top: 0px;
        left: 0px;
        pointer-events: auto !important;
      }
      .user-info-panel {
        padding: 10px 20px 10px;
        background: #fff;
        width: 300px;
        position: absolute;
        top: 60px;
        left: -150px;
        border-radius: 5px;
        box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
        z-index: 10000;
        opacity: 0;
        transition: opacity 0.3s;
        pointer-events: none;
        .nick-name {
          font-size: 16px;
          text-align: center;
          line-height: 40px;
          color: var(--text3);
        }
        .count-info {
          margin: 10px 0px;
          display: flex;
          justify-content: space-around;
          .count-info-item {
            text-align: center;
            .count-title {
              color: var(--text3);
              margin-top: 5px;
            }
            .count-value {
              text-align: center;
              color: var(--text);
            }
          }
        }
        .item {
          font-size: 14px;
          display: block;
          text-align: left;
          line-height: 40px;
          color: var(--text3);
          text-decoration: none;
          display: flex;
          justify-content: space-between;
          padding: 0 15px;
          .item-name {
            flex: 1;
          }
          &::before {
            margin-right: 15px;
          }
          &:hover {
            background: #e8e8e8;
            border-radius: 5px;
          }
        }
        .logout {
          display: block;
          margin-top: 10px;
          cursor: pointer;
        }
      }
      &:hover {
        overflow: visible;
        .avatar {
          transform: scale(2) translateY(10px) translateX(-10px);
        }
        .user-info-panel {
          opacity: 1;
          pointer-events: auto !important;
        }
      }
      // img {
      //   width: 100%;
      //   border-radius: 50%;
      // }
    }
    .user-panel-item {
      text-align: center;
      cursor: pointer;
      padding: 0 13px;
      color: #fff;
      .iconfont {
        text-align: center;
        font-size: 20px;
        font-weight: normal;
      }
    }
    .btn-upload {
      margin-left: 10px;
      .el-button {
        background: #fb7299;
        border-radius: 8px;
        padding: 0 20px;
        border: 0;
        .iconfont {
          &::before {
            margin-right: 5px;
          }
        }
      }
    }
  }
}
.header-bar-light {
  color: #fff;
  .menu {
    color: #fff;
  }
  a {
    color: #fff;
  }
}
.header-bar-dark {
  color: #61666d;
  .menu {
    color: #61666d;
  }
  a {
    color: #61666d;
  }
}

.nav-list {
  display: flex;
  .nav-part {
    &:last-child {
      border-right: none;
    }
    padding: 0px 10px;
    border-right: 1px solid #ddd;
    .nav-item {
      display: flex;
      padding: 0px 10px;
      height: 35px;
      border-radius: 3px;
      cursor: pointer;
      align-items: center;
      width: 150px;
      text-decoration: none;
      color: #2f3238;
      &:hover {
        background: #ddd;
      }
      .icon {
        width: 25px;
        height: 25px;
        overflow: hidden;
        margin-right: 5px;
        img {
          width: 100%;
        }
      }
      .category-name {
        flex: 1;
      }
    }
  }
}
</style>
